const buf=Buffer.from('hello,world','ascii');
console.log(buf);
console.log('-------------------------')
const buf2=Buffer.from('hello,world','utf8');
console.log(buf2);
console.log('-----------------')

const but3=Buffer.from('test');
console.log(but3.toString('hex'));
console.log(but3.toString('utf-8',1,3));//1,3  ---> 开始位置，结果位置

console.log('--------------------')

const but4 =Buffer.from([0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8]);
const json=JSON.stringify(but4);
console.log(json);
console.log('---------------------')

const copy=JSON.parse(json,(key,value)=>{
    return value && value.type==='Buffer'?
        Buffer.from(value.data):
            value;
});
console.log(copy);
console.log('---------------------')

const buf5 =Buffer.alloc(256);
// write方法，返回的是实际的字节数
const len=buf5.write('\u00bd+\u00bc=\u00be',0);
console.log(`${len}个字节长度${buf5.toString('utf-8',0,len)}`);
console.log('------------------');
console.log(`${len}个字节长度${buf5.toString('utf-8',0,5)}`);
console.log('------------------');
console.log(`${len}个字节长度${buf5.toString('utf-8')}`);
console.log('------------------');


const buf6 =Buffer.from([-1,5]);
console.log(buf6.readInt8(0));
console.log(buf6.readInt8(1));
// console.log(buf6.readInt8(2));
console.log('-------------------')


let buffer1=Buffer.from('您好');
let buffer2=Buffer.from('Node.js');
let buffer3=Buffer.concat([buffer1,buffer2],buffer2.length+buffer1.length);
console.log("buffer3 内容："+buffer3.toString()+'长度'+buffer3.length);
console.log('-------------------')
let buffer4=Buffer.concat([buffer1,buffer2],10);
console.log("buffer3 内容："+buffer3.toString()+'长度'+buffer4.length);

console.log('-------------------')

// 复制
let  copy1=Buffer.from('123456789');
let Copy2=Buffer.from('oooooo');
Copy2.copy(copy1,1);
console.log(copy1.toString());
console.log('-------------------')
Copy2.copy(copy1,1,0,3);
console.log(copy1.toString());
console.log('-------------------')

// 剪切
let buffer5=Buffer.from('helloWorld');
let buffer6=buffer5.slice(0,4);
console.log('buffer5 content:'+buffer6.toString());
console.log('-------------------')
buffer6.write('2235',0);
console.log(buffer6.toString());
console.log('-------------------')
console.log(buffer5.toString());
console.log('-------------------')